package com.wolfman.wolfchat.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wolfman.wolfchat.mapper.UserMapper;
import com.wolfman.wolfchat.po.User;
import com.wolfman.wolfchat.service.UserService;
import com.wolfman.wolfchat.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Title
 * @Description
 * @Author WolfMan
 * @Date 2022/1/14 12:56
 * @Email 2370032534@qq.com
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;

    /**
     * 注册用户
     *
     * @param user
     * @return
     */
    @Override
    public Result register(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        List<User> userList = userMapper.selectList(queryWrapper);
        if (!userList.isEmpty()) {
            return Result.error("用户已存在！");
        }
        userMapper.insert(user);
        return Result.success("注册成功！", user);
    }

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("password", user.getPassword());
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据用户id查找用户
     *
     * @param userId
     * @return
     */
    @Override
    public User selectByUserId(Integer userId) {
        User user = userMapper.selectById(userId);
        user.setPassword(null);
        return user;
    }
}
